package cn.portx.frame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf3.dorado.jpa.JpaUtil;
import com.bstek.bdf3.saas.Constants;
import com.bstek.bdf3.saas.SaasUtils;
import com.bstek.bdf3.saas.command.Command;
import com.bstek.bdf3.saas.command.CommandNeedReturn;
import com.bstek.bdf3.security.ContextUtils;
import com.bstek.bdf3.security.orm.User;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.base.tab.ControlTab;
import com.bstek.dorado.web.DoradoContext;

import cn.portx.entity.Message;
import cn.portx.frame.entity.MineNotice;
import cn.portx.frame.entity.Notice;

@Component
public class MainPR {

	public void onInit(View view) {
//		DoradoContext.getCurrent().setAttribute(DoradoContext.SESSION, ContextHolder.SESSION_USER_KEY, ContextUtils.getLoginUser());

		ControlTab tab = (ControlTab) view.getViewElement("noticeTab");
		if (!Constants.MASTER.equals(ContextHolder.getLoginOrgId())) {
			tab.setVisible(true);
		} else {
			tab.setVisible(false);
		}

	}

	@DataProvider
	public void loadMessage(Page<Message> page, Criteria criteria, int type) {
		User user = ContextHolder.getLoginUser();
		synchronized (SaasUtils.class) {
			SaasUtils.doQuery(ContextHolder.getLoginOrgId(), new Command() {

				@Override
				public void execute() {
					JpaUtil.linq(Message.class)
						.addIf(type == 0)
							.equal("receiver", user.getUsername())
							.equal("receiverDel", 0)
						.endIf()
						.addIf(type == 1)
							.equal("sender", user.getUsername())
							.equal("senderDel", 0)
						.endIf()
						.where(criteria)
						.addIf(type == 0)
							.asc("read")
							.desc("createDate")
						.endIf()
						.addIf(type == 1)
							.desc("createDate")
						.endIf()
						.paging(page);
				}

			});
		}
	}

	@DataProvider
	public void loadNotice(Page<Notice> page, Criteria criteria) {
		User user = ContextHolder.getLoginUser();
		synchronized (SaasUtils.class) {
			SaasUtils.doNonQuery(new Command() {

				@Override
				public void execute() {
					JpaUtil.linq(MineNotice.class).equal("receiver", user.getUsername()).where(criteria).asc("read").desc("createDate").paging(page);
				}

			});
		}
	}

	@Expose
	public Long[] fetchMessageCount() {
		User user = ContextHolder.getLoginUser();
		synchronized (SaasUtils.class) {
			Long count1 = SaasUtils.doNonQuery(ContextHolder.getLoginOrgId(), new CommandNeedReturn<Long>() {

				@Override
				public Long execute() {
					Long count = JpaUtil.linq(Message.class).equal("receiver", user.getUsername()).equal("receiverDel", 0).equal("read", 0).count();
					return count;
				}
			});
			Long count2 = 0l;
			if (!Constants.MASTER.equals(ContextHolder.getLoginOrgId())) {
				count2 = fetchNoticeCount();
			}
			return new Long[] { count1, count2 };
		}
	}

	private long fetchNoticeCount() {
		User user = ContextHolder.getLoginUser();
		return SaasUtils.doNonQuery(new CommandNeedReturn<Long>() {

			@Override
			public Long execute() {
				Long count = JpaUtil.linq(MineNotice.class).equal("receiver", user.getUsername()).equal("read", 0).count();
				return count;
			}
		});
	}

	@Expose
	@Transactional
	public void setRead(List<String> ids) {
		JpaUtil.linu(Message.class).set("read", 1).in("id", ids.toArray()).update();
	}

	@Expose
	@Transactional
	public void delMsg(List<String> ids, int type) {
		JpaUtil.linu(Message.class).addIf(type == 0).set("receiverDel", 1).endIf().addIf(type == 1).set("senderDel", 1).endIf().in("id", ids.toArray()).update();
	}

	@Expose
	public void setReadNotice(List<String> ids) {
		synchronized (SaasUtils.class) {
			SaasUtils.doNonQuery(new Command() {

				@Override
				public void execute() {
					JpaUtil.linu(MineNotice.class).set("read", 1).in("id", ids.toArray()).update();

				}

			});
		}
	}

	@Expose
	public void delNotice(List<String> ids) {
		synchronized (SaasUtils.class) {
			SaasUtils.doNonQuery(new Command() {

				@Override
				public void execute() {
					JpaUtil.lind(MineNotice.class).in("id", ids.toArray()).delete();

				}

			});
		}
	}

	@Expose
	@Transactional
	public void sendMessage(String content, List<String> receivers) {
		User user = ContextHolder.getLoginUser();
		List<Message> list = new ArrayList<Message>();
		for (String receiver : receivers) {
			Message message = new Message();
			message.setContent(content);
			message.setReceiver(receiver);
			message.setSender(user.getUsername());
			message.setType(1);
			list.add(message);
		}
		JpaUtil.persist(list);
	}

	@DataProvider
	@Transactional(readOnly = true)
	public void loadUsers(Page<User> page, Map<String, Object> params) {
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		String filterValue = (String) params.get("filterValue");
		JpaUtil.linq(User.class).equal("enabled", true).addIf(filterValue).or().like("username", filterValue + "%").like("nickname", filterValue + "%").end().endIf().paging(page);
	}
}
